// pages/ownerCertification/ownerCertification.js
const host = require('../../config.js').host;
const network = require('../../utils/network.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    host: host,
    storePhotoList: [],
    tradingCertificateList: [],
    formData:{}
  },

  /**
   * 业主名称输入
   */
  nameInput(e) {
    let formData = this.data.formData;
    formData.name = e.detail.value;
    this.setData({
      formData: formData
    })
  },
  /**
   * 业主描述输入
   */
  detailInput(e) {
    let formData = this.data.formData;
    formData.detail = e.detail.value;
    this.setData({
      formData: formData
    })
  },
  /**
   * 业主图片选择
   */
  chooseStorePhotos() {
    let that = this;
    let formData = this.data.formData;
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      success: function (res) {
        network.uploadFileLoading("/common/upload", res.tempFiles[0].path, res => {
          let data = JSON.parse(res.data);
          let servicePath = data.fileName;
          let imgUrl = host + servicePath;
          formData.storePhotos = servicePath;
          that.setData({
            formData: formData,
            storePhotoList: [imgUrl]
          })
        })
      }
    });
  },
  /**
   * 业主证书选择
   */
  chooseTradingCertificate() {
    let that = this;
    let formData = this.data.formData;
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      success: function (res) {
        network.uploadFileLoading("/common/upload", res.tempFiles[0].path, res => {
          let data = JSON.parse(res.data);
          let servicePath = data.fileName;
          let imgUrl = host + servicePath;
          formData.tradingCertificate = servicePath;
          that.setData({
            formData: formData,
            tradingCertificateList: [imgUrl]
          })
        })
      }
    });
  },
  /**
   * 查看业主图片
   */
  viewStorePhoto(e) {
    wx.previewImage({
      urls: this.data.storePhotoList,
      current: e.currentTarget.dataset.url
    });
  },
  /**
   * 查看业主证书
   */
  viewTradingCertificate(e) {
    wx.previewImage({
      urls: this.data.tradingCertificateList,
      current: e.currentTarget.dataset.url
    });
  },

  DelImg(e) {
    let type = e.currentTarget.dataset.type;
    wx.showModal({
      title: '删除图片',
      content: '确定要删除这张图片吗？',
      cancelText: '取消',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          if(type == 1){
            let imgList = this.data.storePhotoList;
            imgList.splice(e.currentTarget.dataset.index, 1);
            this.setData({
              storePhotoList: imgList
            })
          }
          if(type == 2){
            let imgList = this.data.tradingCertificateList;
            imgList.splice(e.currentTarget.dataset.index, 1);
            this.setData({
              tradingCertificateList: imgList
            })
          }
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})